'''
This is a program to solve equations for two or even higher degree.
However, only quadratic one has been finished so far.

@Owner dYrolign and DERFDY 2025.
@Author dYrolign.
@Contributor iKeinsan, Werstog, Leuitsenn. Actually those are all myself. Please do not mind and do not spread outside. ;D
@Special Thanks Deepseek for generating regular expression.
@Date 2025/9/23
'''

from math import sqrt as sqrt
from math import pow as pow
import re

# This function is to split strings through specific markers.
def split(text):
    a,b,c = 0,0,0

    pattern = r'^\s*(?P<a>[+-]?\d*)x\^2\s*(?P<b>[+-]\d*)x\s*(?P<c>[+-]\d+)\s*=\s*0\s*$'
    
    match = re.search(pattern, text)

    if match:
        a, b, c = match.groups()
        a = 1 if a == '' else -1 if a == '-' else float(a)
        b = float(b)
        c = float(c)
        
        return [a, b, c]
    else:
        print("Oops! Currently we haven't supported this one yet.")
    exit(0)

def solve():
    equation = input("Now input an equation here in the form of \"ax^2+bx+c=0\", such as\"2x^2-3x-1=0\".\nNot supporting the situation where a single term and a constant term are missing yet.\n")
    abc = split(equation)
    a,b,c = abc[0],abc[1],abc[2]
    delta = pow(b,2)-4*a*c
    try:
        x1,x2 = (-b-sqrt(delta))/(2*a),(-b+sqrt(delta))/(2*a)
        print("One root of the equation is"+str(x1))
        print("One root of the equation is"+str(x2))
    except ValueError:
        x1,x2 =  (-b-sqrt(-delta))/(2*a),(-b+sqrt(-delta))/(2*a)
        print("One root of the equation is"+str(x1)+"i")
        print("One root of the equation is"+str(x2)+"i")

# Now the program begins
print("Welcome to use the Equation Solving Master!")
print("This is a program to solve equations for two or even higher degree.\nHowever, only quadratic one has been finished so far.\n")
solve()

